package com.vay.de.dang.online.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.vay.de.dang.online.web.ReqResult;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.AddressEntity;
import com.vay.de.dang.online.web.TableApi;
import com.vay.de.dang.online.web.ReqCallback;

public class DialogAddress extends DialogBase {

    private ListView pListView;
    private ListView cListView;
    private ListView rListView;
    private OnCityClick itemClick;
    private RegionAdapter provinceAdapter;
    private RegionAdapter cityAdapter;
    private RegionAdapter regionAdapter;
    private LayoutInflater inflater;

    private AddressEntity province;
    private AddressEntity city;
    private AddressEntity region;

    public DialogAddress(Context context) {
        super(context);
        super.setContentView(R.layout.dialog_city_choice);
        autoWidth(1.0f);

        inflater = getLayoutInflater();
        pListView = findViewById(R.id.p_l);
        cListView = findViewById(R.id.c_l);
        rListView = findViewById(R.id.r_l);

        provinceAdapter = new RegionAdapter(1);
        cityAdapter = new RegionAdapter(2);
        regionAdapter = new RegionAdapter(3);

        pListView.setAdapter(provinceAdapter);
        cListView.setAdapter(cityAdapter);
        rListView.setAdapter(regionAdapter);

        findViewById(R.id.bnt_close).setOnClickListener(view -> {
            dismiss();
        });
    }

    public DialogAddress setOnCityClick(OnCityClick click) {
        itemClick = click;
        return this;
    }

    public void showCity(Activity activity) {
        TableApi.provinceTable(new ReqCallback(activity) {
            @Override
            public void callback(ReqResult value) {
                if (value.isOk()) {
                    List<AddressEntity> list = (List<AddressEntity>) value.result;
                    provinceAdapter.loadData(list);
                    loadCity(list.get(0));
                }
                show();
            }
        }.showDialog(true));
    }

    private void loadCity(AddressEntity province) {
        TableApi.regionTable(2, province.getAddressCode(), new ReqCallback() {
            @Override
            public void callback(ReqResult value) {
                if (value.isOk()) {
                    List<AddressEntity> list = (List<AddressEntity>) value.result;
                    cityAdapter.loadData(list);
                    loadRegion(list.get(0));
                }
            }
        });
    }

    private void loadRegion(AddressEntity city) {
        TableApi.regionTable(3, city.getAddressCode(), new ReqCallback() {
            @Override
            public void callback(ReqResult value) {
                if (value.isOk()) {
                    List<AddressEntity> list = (List<AddressEntity>) value.result;
                    regionAdapter.loadData(list);
                }
            }
        });
    }

    private class RegionAdapter extends BaseAdapter {

        final int colorPre = Color.parseColor("#43AE63");
        final int colorNor = Color.parseColor("#999999");

        private int mType;
        private List<AddressEntity> mList = new ArrayList<>();

        public RegionAdapter(int type) {
            mType = type;
        }

        public void loadData(List<AddressEntity> list) {
            if (list == null) return;
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public AddressEntity getItem(int i) {
            return mList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int index, View view, ViewGroup viewGroup) {
            AddressEntity entity = getItem(index);
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.item_address, null);
                holder.tv = view.findViewById(R.id.name_tv);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.tv.setText(entity.getAddressName());
            if (mType == 1) {
                if (province != null && province.getAddressCode().equals(entity.getAddressCode())) {
                    holder.tv.setTextColor(colorPre);
                    holder.tv.setTypeface(Typeface.DEFAULT_BOLD);
                } else {
                    holder.tv.setTextColor(colorNor);
                    holder.tv.setTypeface(Typeface.DEFAULT);
                }
            } else if (mType == 2) {
                if (city != null && city.getAddressCode().equals(entity.getAddressCode())) {
                    holder.tv.setTextColor(colorPre);
                    holder.tv.setTypeface(Typeface.DEFAULT_BOLD);
                } else {
                    holder.tv.setTextColor(colorNor);
                    holder.tv.setTypeface(Typeface.DEFAULT);
                }
            }

            view.setOnClickListener(v -> {
                if (mType == 1) {
                    province = entity;
                    city = null;
                    region = null;
                    loadCity(entity);
                    provinceAdapter.notifyDataSetChanged();
                } else if (mType == 2) {
                    city = entity;
                    region = null;
                    loadRegion(entity);
                    cityAdapter.notifyDataSetChanged();
                } else if (mType == 3) {
                    region = entity;
                    if (itemClick != null && province != null && city != null) {
                        itemClick.onCity(province, city, region);
                        dismiss();
                    }
                }
            });

            return view;
        }

    }

    private class ViewHolder {
        public TextView tv;
    }

    public interface OnCityClick {

        void onCity(AddressEntity province, AddressEntity city, AddressEntity region);
    }
}
